package com.lvyq.javaprint.util;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author lvyq PDF打印
 * @version 1.0
 * Create by 2024-05-06 14:00
 */
public class PDFPrintUtils {


    /**
     * @description 获取document
     * @author lvyq
     * @throws
     * @return PDDocument
     * @time 2024-08-07 15:29
     */
    public static PDDocument getPDDocument(URL url){
        try {
            URLConnection  connection = url.openConnection();
            // 如果需要的话，可以设置连接超时或读取超时等参数
            InputStream pdfInputStream = connection.getInputStream();
            PDDocument document = PDDocument.load(pdfInputStream);
            pdfInputStream.close();
            return document;
        } catch (IOException e) {
            throw new RuntimeException("获取文件异常");
        }

    }

    /**
     * @description 获取文件
     * @author lvyq
     * @param[1] multipartFile
     * @throws
     * @return PDDocument
     * @time 2024-08-07 17:02
     */
    public static PDDocument getPDDocument(File file){
        try {
            PDDocument document = PDDocument.load(file);
            return document;
        } catch (IOException e) {
            throw new RuntimeException("获取文件异常");
        }
    }

    public static PDDocument getPDDocument(MultipartFile multipartFile){
        try {
            PDDocument document = PDDocument.load(multipartFile.getInputStream());
            return document;
        } catch (IOException e) {
            throw new RuntimeException("获取文件异常");
        }
    }


}
